﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BudgetApp.Adapters;
using BudgetApp.Data;
using BudgetApp.Data.Models;

namespace BudgetApp.Controllers
{
    [Authorize]
    public class SubCategoriesController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        private IBudgetAdapter _budgetAdapter;

        public SubCategoriesController()
        {
            _budgetAdapter = BudgetAdapterFactory.GetBudgetAdapter();
        }

        public SubCategoriesController(IBudgetAdapter budgetAdapter)
        {
            _budgetAdapter = budgetAdapter;
        }

        // GET: SubCategories
        public ActionResult Index()
        {
            return View(db.SubCategories.ToList());
        }
        
        // GET: SubCategories/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: SubCategories/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "SubCategoryId,SubCategoryType")] SubCategory subCategory)
        {
            if (ModelState.IsValid)
            {
                db.SubCategories.Add(subCategory);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(subCategory);
        }

        // GET: SubCategories/Edit/id
        public ActionResult Edit(int id)
        {
            var subCategory = _budgetAdapter.GetSubCategoryDetails(id);
            if (subCategory == null)
            {
                return HttpNotFound();
            }
            return View(subCategory);
        }

        // POST: SubCategories/Edit/id
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "SubCategoryId,SubCategoryType")] SubCategory subCategory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(subCategory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(subCategory);
        }

        // GET: SubCategories/Delete/id
        public ActionResult Delete(int id)
        {
            var subCategory = _budgetAdapter.GetSubCategoryDetails(id);
            if (subCategory == null)
            {
                return HttpNotFound();
            }
            return View(subCategory);
        }

        // POST: SubCategories/Delete/id
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var subCategory = _budgetAdapter.GetSubCategoryDetails(id);
            db.SubCategories.Remove(subCategory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
